package ebusiness.ebusiness.Dao.GoodsDao;

import ebusiness.ebusiness.Dao.DBManager;
import ebusiness.ebusiness.Entity.Classify;
import ebusiness.ebusiness.Entity.Good;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class GoodQueryVO {

       public List<Good> getAllGoods() throws  Exception{
           SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
           GoodQuery goodQuery=sqlSession.getMapper(GoodQuery.class);
           List<Good> goodList=goodQuery.getAllGoods();
           sqlSession.close();
           return goodList;
        }

   public Good getGoodByID(int id) throws Exception{
       SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
           try {
               GoodQuery goodQuery=sqlSession.getMapper(GoodQuery.class);
               Good good=goodQuery.getGoodByID(id);
               sqlSession.close();
               return good;
           }catch (Exception e){
               sqlSession.close();
           }

        return null;
   }
   //商品分类的增删改查
    public int addAClassify(int classify_id,int classify_parent_id,String classify_description,String classify_createtime)throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
           try{
               GoodQuery goodQuery=sqlSession.getMapper(GoodQuery.class);
               goodQuery.addAClassify(classify_id,classify_parent_id,classify_description,classify_createtime);
               sqlSession.commit();
               return 1;
           }
            catch (Exception e){
               sqlSession.close();
               return 0;
            }
            finally {
               sqlSession.close();
           }

    }

    public int modifyClassify(int classify_id,int classify_parent_id,String classify_description,String classify_createtime)throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            GoodQuery goodQuery=sqlSession.getMapper(GoodQuery.class);
            goodQuery.modifyClassify(classify_id,classify_parent_id,classify_description,classify_createtime);
            sqlSession.commit();
            sqlSession.close();
            return 1;
        }
        catch (Exception e){
            sqlSession.close();
            return 0;
        }

    }

    public int deleteClassify(int Classid)throws Exception{
           SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
           try {
               GoodQuery goodQuery=sqlSession.getMapper(GoodQuery.class);
               goodQuery.deleteClassify(Classid);
               sqlSession.commit();
               sqlSession.close();
               return 1;
           }
           catch (Exception e){
               sqlSession.close();
               return 0;
           }
    }
    public List<Classify> getAllclassify() throws Exception{

        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();

        try {
            GoodQuery goodQuery=sqlSession.getMapper(GoodQuery.class);
            List<Classify> cs=goodQuery.getAllclassify();
            sqlSession.close();
            return cs;
        }
        catch (Exception e){
            sqlSession.close();
            throw e;
        }

    }
    public List<Classify> getclassifybyID(int id)throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();

        try {
            GoodQuery goodQuery=sqlSession.getMapper(GoodQuery.class);
            List<Classify> cs=goodQuery.getClassifyByid(id);
            sqlSession.close();
            return cs;
        }
        catch (Exception e){
            sqlSession.close();
            throw e;
        }
    }
    public List<Classify> getclassifybyPID(int pid) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();

        try {
            GoodQuery goodQuery=sqlSession.getMapper(GoodQuery.class);
            List<Classify> cs=goodQuery.getClassifyByPid(pid);
            sqlSession.close();
            return cs;
        }
        catch (Exception e){
            sqlSession.close();
            throw e;
        }

    }

}
